/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rofl;

import dk.eleit.input.InputManager;
import dk.rofl.graphics.GameDisplayManager;
import dk.rofl.modes.Mode;
import dk.rofl.modes.ModeSwitcher;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

/**
 *
 * @author menderleit
 */
public class RoFL implements Runnable {

    public static final boolean SHOW_DEBUG = true;
    public static final int SCREEN_WIDTH = 1024;
    public static final int SCREEN_HEIGHT = 768;

    public RoFL() {
        ModeSwitcher.init();
        GameDisplayManager.init(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, 60), "Lords of Raging Flames", false);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RoFL app = new RoFL();
        Thread t = new Thread(app);
        t.start();
    }

    @Override
    public void run() {

        // Timer variables for timing the animation depending on the time between frames.
        long difTime;
        long oldTime = Utils.hitimer();

        // FPS counter variables.
        int fpsTime = 1000;
        int fpsCount = 0;
        int fps = 0;

        // Get a reference to the KeyManager.
        InputManager im = GameDisplayManager.getInputManager();

        // Get the initial mode object.
        Mode mode = ModeSwitcher.getCurrentMode();

        // MAIN LOOP
        while (GameDisplayManager.getJFrame().isShowing()) {
            // difTime = the difference between this and last frame in milliseconds.
            difTime = Utils.hitimer() - oldTime;
            oldTime = Utils.hitimer();

            // Update the FPS counter.
            fpsTime -= difTime;
            if (fpsTime <= 0) {
                fpsTime += 1000;
                fps = fpsCount;
                fpsCount = 0;
            }
            fpsCount++;

            // Update the current mode if needed.
            if (ModeSwitcher.isNewModeSet()) {
                mode = ModeSwitcher.switchMode();
            }

            // Check the controls and do updates based on the current mode.
            mode.controls(im);
            mode.update(difTime);

            // Get a graphics object to the back-buffer.
            Graphics2D g = GameDisplayManager.getGraphics();

            // Clear the screen.
            g.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

            // Draw screen based on the current mode.
            mode.draw(g);

            // Show the Frames Per Second.
            if (SHOW_DEBUG) {
                g.setColor(Color.white);
                String sFPS = "FPS: " + fps;
                int c = (sFPS.length() * 6) >> 1;
                g.drawString("FPS: " + fps, (SCREEN_WIDTH >> 1) - c, SCREEN_HEIGHT - 3);
            }

            // Copy the back-buffer to the front-buffer. (Show the stuff you've drawn)
            GameDisplayManager.show();

            // Dispose of the graphics object.
            g.dispose();
        }

        // Shutdown.
        GameDisplayManager.exit();
        System.exit(0);
    }
}
